unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure btnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  n = 16;
  lx = 42;   //43X30
  ly = 30;
var
  Form1: TForm1;
  f: boolean;
  tu, sy: TBitMap;
  map: Array[0..n-1, 0..n-1] of Byte;
  h: hcursor;

implementation

{$R *.dfm}

procedure btnCreate;
var
  btn: TSpeedButton;
  i, j: integer;
begin
  for i := 0 to n - 1 do
    for j := 0 to n - 1 do map[i,j] := 0;
  Form1.ClientWidth := round( n*lx );
  Form1.ClientHeight := round( n*ly );
  tu := TBitmap.Create;
  sy := TBitmap.Create;
  tu.LoadFromFile( 'tu.bmp' );
  sy.LoadFromFile( 'syria.bmp' );
  h := LoadCursorFromFile('curTu.cur');
  Screen.Cursors[1] := h;
  h := LoadCursorFromFile('curSy.cur');
  Screen.Cursors[2] := h;
  Form1.Cursor := 1;

  for i := 0 to n*n - 1 do begin
    btn := TSpeedButton.Create( Application );
    btn.Width := lx;
    btn.Height := ly;
    btn.Top := (i div n)*ly;
    btn.Left := (i mod n)*lx;
    //btn.Tag := i;
    btn.Parent := Form1;

    btn.Name := 'btn'+IntToStr(i);
    btn.Caption := '';
    btn.Tag := i;
    btn.OnClick := Form1.BtnClick;
    btn.Glyph := nil;
    btn.Tag := 0;
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  btnCreate;
end;



procedure win( player: byte );
var i, j: integer;
begin
  for i := 0 to n - 1 do
    for j := 0 to n - 1 do begin
      map[i,j] := 0;
    end;
  ShowMessage( 'Player ' + IntToStr( player ) + ' win!' );
  //Form1.Destroy;
end;

procedure TForm1.btnClick(Sender: TObject);   // div = y     mod = x
const kw = 4;
var
  i, j: Integer;
  x, y, w: integer;
begin
  w := 0;
  if (Sender as TSpeedButton).Tag = 0 then begin
    x := (Sender as TSpeedButton).Left div lx;
    y := (Sender as TSpeedButton).Top div ly;

    if f then begin
      (Sender as TSpeedButton).Glyph := sy;
      Form1.Cursor := 1;
      (Sender as TSpeedButton).Tag := 1;
       map[x,y] := 1;
    end else begin
      (Sender as TSpeedButton).Glyph := tu;
      Form1.Cursor := 2;
      (Sender as TSpeedButton).Tag := 2;
      map[x,y] := 2;
    end;
    f := not f;

    ////////////////////////////////////////////////
    for i := 1 to kw do
      if map[i+x,y] = 1 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,y] = 1 then inc( w ) else break;

    if w >= kw then begin
      win(1);
      exit;
    end else w := 0;
               //---------------------------------------
    for i := 1 to kw do
      if map[x,i+y] = 1 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[x,i+y] = 1 then inc( w ) else break;

    if w >= kw then begin
      win(1);
      exit;
    end else w := 0;
               //----------------------------------------
    for i := 1 to kw do
      if map[i+x,i+y] = 1 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,i+y] = 1 then inc( w ) else break;

    if w >= kw then begin
      win(1);
      exit;
    end else w := 0;
               //-----------------------------------------
    for i := 1 to kw do
      if map[i+x,y-1] = 1 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,y-i] = 1 then inc( w ) else break;

    if w >= kw then begin
      win(1);
      exit;
    end else w := 0;
    //////////////////////////////////////////////
    ////////////////////////////////////////////////
    for i := 1 to kw do
      if map[i+x,y] = 2 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,y] = 2 then inc( w ) else break;

    if w >= kw then begin
      win(2);
      exit;
    end else w := 0;
               //---------------------------------------
    for i := 1 to kw do
      if map[x,i+y] = 2 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[x,i+y] = 2 then inc( w ) else break;

    if w >= kw then begin
      win(2);
      exit;
    end else w := 0;
               //----------------------------------------
    for i := 1 to kw do
      if map[i+x,i+y] = 2 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,i+y] = 2 then inc( w ) else break;

    if w >= kw then begin
      win(2);
      exit;
    end else w := 0;
               //-----------------------------------------
    for i := 1 to kw do
      if map[i+x,y-1] = 2 then inc( w ) else break;
    for i := - 1 downto - kw do
      if map[i+x,y-i] = 2 then inc( w ) else break;

    if w >= kw then begin
      win(2);
      exit;
    end else w := 0;
    //////////////////////////////////////////////

  end;
end;

end.
